/**
 * 定义监测仪器管理模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.voc.vocQyManager.vocInfo.monitorEquipmentManager', ['water.apps.common.stationInfo.monitorEquipmentManager','water.entity.monitorStation']);
	
	module.controller('vocmonitorEquipmentManagerController',function($rootScope,$scope,$monitorEquipmentService,$controller,$monitorStationService){
        
		/**
		 * 选择的污水处理厂id
		 */
		$scope.COMPANY_CODE;
		/**
         * 站点数据
         */
		$scope.stationSelectList=null;
		var control = $controller("commonMonitorEquipmentManagerController",{$scope:$scope});
		$scope.dataPramChangedBroadCast();
		/**
		 * 选择站点
		 */
		$scope.onStationChange=function(event){
			if(!event){
				
			}
			else{
				$scope.stationModel.STATION_NAME=event.v;
				$scope.stationModel.STATION_ID=event.k;
			}
		};
		/**
		 * 新增按钮点击事件
		 */
		$scope.onCreateClick = function(){
			$scope.equipmentCreate ={};
			//赋初始值，默认第一个站点
			$scope.equipmentCreate.MONITOR_POINT = $scope.stationSelectList[0].k;
			$scope.stationModel.STATION_NAME=$scope.stationSelectList[0].v;
			$scope.stationModel.STATION_ID=$scope.stationSelectList[0].k;
			$scope.createDialog.open();
		}
		/**
		 * 提交新建按钮点击事件
		 */
		$scope.onSubmitCreateClick = function(data){
			data = $scope.updatePollListStationId(data);
			//判断是否已存在该仪器
			if($scope.equipments.length>0){
				for(var i=0;i<$scope.equipments.length;i++){
					var obj=$scope.equipments[i];
					if(obj.POLLNUM==data.MONITOR_POLLUTANTS){
						$.messager.alert("站点已存在该设备,不能重复设置!","站点已存在该设备,不能重复设置!",'info');
						$scope.createDialog.close();
						return;
					}
				}
			};
			$monitorEquipmentService.createModel(data).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				$scope.createDialog.close();
				$scope._getEquipmentList();
			});
		};
		/**
		 * 编辑按钮点击事件
		 */
		$scope.onEditClick = function(data){
			$scope.stationModel.STATION_NAME=data.SITE_NAME;
			$monitorEquipmentService.getModel(data.GUID).then(function(d){
				$scope.equipmentEdit = d;
				$scope.editDialog.open();
			});
		};
		/**
		 * 提交编辑按钮点击事件
		 */
		$scope.onSubmitEditClick = function(data){
			data = $scope.updatePollListStationId(data);
			//判断是否已存在该仪器
			if($scope.equipments.length>0){
				for(var i=0;i<$scope.equipments.length;i++){
					var obj=$scope.equipments[i];
					if(obj.POLLNUM==data.MONITOR_POLLUTANTS){
						$.messager.alert("站点已存在该设备,不能重复设置!","站点已存在该设备,不能重复设置!",'info');
						$scope.createDialog.close();
						return;
					}
				}
			};
			$monitorEquipmentService.updateModel(data).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				$scope._getEquipmentList();
				$scope.editDialog.close();
			});
			
		}
		/**
		 * 监听站点列表点击事件
		 */
		if($wdp.viewValue.wwtpDetailParmWatch){
			$wdp.viewValue.wwtpDetailParmWatch();
		}
		$wdp.viewValue.wwtpDetailParmWatch = $rootScope.$watch(function(){return $wdp.viewValue.wwtpDetailParm;},function(){
			if(!$wdp.viewValue.wwtpDetailParm){
				return;
			}
			if($wdp.viewValue.wwtpDetailParm.COMPANY_CODE){
			$scope.COMPANY_CODE = $wdp.viewValue.wwtpDetailParm.COMPANY_CODE;
			$scope._getEquipmentList ();
			$scope._getStationSelectList();
		  }
        },true);
		/**
		 * 获取仪器列表
		 */
		$scope._getEquipmentList=function(){
			$monitorEquipmentService.getList({WWTP_ID:$scope.COMPANY_CODE}).then(function(d){
				$scope.equipments = d
			});
		};
		/**
		 * 获取站点列表，编辑或者新增的时候需要选择
		 */
		$scope._getStationSelectList = function(){
			$monitorStationService.getDomainList({WWTP_ID:$scope.COMPANY_CODE}).then(function(d){
				$scope.stationSelectList = d;
				$scope.stationSelecter.setData(d);
				$scope.stationSelecterEdit.setData(d);
			});
		};
		
		/**
		 * 新增按钮点击事件
		 */
		$scope.onCreateClick = function(){
			if($scope.stationSelectList==null||$scope.stationSelectList.length==0) {
				$.messager.alert("不存在站点信息，无法新建监测设备","不存在站点信息，无法新建监测设备",'info');
				return;
			}
			$scope.equipmentCreate ={};
			//赋初始值，默认第一个站点
			$scope.equipmentCreate.MONITOR_POINT = $scope.stationSelectList[0].k;
			$scope.stationModel.STATION_NAME=$scope.stationSelectList[0].v;
			$scope.stationModel.STATION_ID=$scope.stationSelectList[0].k;
			$scope.createDialog.open();
		}
		
	});
}(window, angular));	